//
//  TravelTactic_MainViewController.m
//  BTRIP
//
//  Created by Rain on 15/11/13.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "TravelTactic_MainViewController.h"
#import "TravelTactic_MainCollectionViewCell.h"
#import "TravelTactic_MainCollectionReusableView.h"
#import "TravelTactic_MainModel.h"
#import "TravelTactic_MainModel_Big.h"
#import "DataHandle.h"
#import <MBProgressHUD.h>
#import "MBHudTool.h"
#import "TravelTactic_AreaViewController.h"

@interface TravelTactic_MainViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) TravelTactic_MainModel *travelTactic_MainModel;

@property (nonatomic, retain) TravelTactic_MainModel_Big *travelTactic_MainModel_Big;

@property (nonatomic, retain) UISegmentedControl *segmentedControl;

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *modelArr;

@property (nonatomic, retain) NSArray *sectionTitleArr;

@property (nonatomic, retain) UISegmentedControl *segmented;

@property (nonatomic, retain) UIView *navigationView;

@property (nonatomic, retain) UILabel *titleLabel;

@property (nonatomic, retain) MBProgressHUD *hud;

/**
 *  国外数组
 */
@property (nonatomic, retain) NSMutableArray *outArr;

/**
 *  国内数组
 */
@property (nonatomic, retain) NSMutableArray *inArr;

@end

@implementation TravelTactic_MainViewController

- (void)dealloc
{
    [_hud release];
    [_titleLabel release];
    [_navigationView release];
    [_sectionTitleArr release];
    [_segmented release];
    [_inArr release];
    [_outArr release];
    [_segmentedControl release];
    [_collectionView release];
    [_modelArr release];
    
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [self.navigationController.navigationBar setHidden:NO];
//    self.navigationController.navigationBar.alpha = 0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArr = [[NSMutableArray alloc] init];
    self.inArr = [[NSMutableArray alloc] init];
    self.outArr = [[NSMutableArray alloc] init];
    self.sectionTitleArr = [[NSArray alloc] init];
    
    self.sectionTitleArr = @[@"亚洲", @"欧洲", @"美洲", @"台港澳", @"大陆"];
    
    self.navigationItem.title = @"旅行攻略";
    
    self.hud = [MBProgressHUD HUDForView:self.collectionView];
    
    [self dataload:@"http://chanyouji.com/api/destinations.json?page=1"];
    
//    [self createSegmentedView];
    
    [self createUI];
}

- (void)dataload:(NSString *)url {
    
    [DataHandle contectWithURL:url andParameters:nil andViewController:self andBlock:^(id response) {
        
        for (NSDictionary *tempDic in response) {
            TravelTactic_MainModel_Big *bigModel = [[TravelTactic_MainModel_Big  alloc] init];
            [bigModel setValuesForKeysWithDictionary:tempDic];
            if ([bigModel.category isEqualToString:@"1"]) {
                
                [self.modelArr addObject:bigModel.destinationsArr];
                
            } else if ([bigModel.category isEqualToString:@"2"]) {
                
                [self.modelArr addObject:bigModel.destinationsArr];
                
            } else if ([bigModel.category isEqualToString:@"3"]) {
                
                [self.modelArr addObject:bigModel.destinationsArr];
                
            } else if ([bigModel.category isEqualToString:@"99"]) {
                
                [self.modelArr addObject:bigModel.destinationsArr];
                
            } else if ([bigModel.category isEqualToString:@"999"]) {
                
                [self.modelArr addObject:bigModel.destinationsArr];
                
            }
        }
        [self.collectionView reloadData];
        [MBHudTool dissmissHUD:self.hud view:self.collectionView];
    }];
    [MBHudTool showHUD:nil view:self.collectionView hud:self.hud];
}

//- (void)createSegmentedView {
//    
//    NSArray *titleArr = @[@"国外", @"国内"];
//    self.segmented = [[UISegmentedControl alloc] initWithItems:titleArr];
//    self.segmented.frame = CGRectMake(10 * SWIDTH, 64 * SWIDTH, KWIDTH - 20 * SWIDTH, 30 * SWIDTH);
//    self.segmented.tintColor = [UIColor lightGrayColor];
//    [self.segmented addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
//    self.segmented.selectedSegmentIndex = 0;
//    [self.view addSubview:self.segmented];
//    [self.segmented release];
//    
//}

- (void)createUI {
    
    
    self.navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, 64)];
    self.navigationView.backgroundColor = [UIColor brownColor];
    [self.view addSubview:self.navigationView];
    [self.navigationView release];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KWIDTH - KWIDTH / 3 * 2, 20, KWIDTH / 3, 44 * SWIDTH)];
    self.titleLabel.text = @"旅行攻略";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor whiteColor];
    [self.navigationView addSubview:self.titleLabel];
    [self.titleLabel release];
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 5 * SWIDTH;
    flowLayout.minimumLineSpacing = 10 * SWIDTH;
    flowLayout.itemSize = CGSizeMake((KWIDTH - 30 * SWIDTH) / 2, KHEIGHT / 3);
    flowLayout.sectionInset = UIEdgeInsetsMake(10 * SWIDTH, 10 * SWIDTH, 10 * SWIDTH, 10 * SWIDTH);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT) collectionViewLayout:flowLayout];
    self.collectionView.contentInset = UIEdgeInsetsMake(64 * SWIDTH, 0, 0, 0);
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    [self.collectionView release];
    [flowLayout release];
    
    // 注册cell
    [self.collectionView registerClass:[TravelTactic_MainCollectionViewCell class] forCellWithReuseIdentifier:@"travelTactic_MainCollectionViewCell"];
    
    // 注册头视图
    [self.collectionView registerClass:[TravelTactic_MainCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"travelTactic_MainCollectionHeaderView"];
    
}



#pragma mark 返回单元格数量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    for (int i = 0; i < _modelArr.count; i ++) {
        if (section == i) {
            return [_modelArr[i] count];
        }
    }
    return 0;
}

#pragma mark 返回单元格数据
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TravelTactic_MainCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"travelTactic_MainCollectionViewCell" forIndexPath:indexPath];
    cell.travelTactic_MainModel = [[_modelArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}

#pragma mark 单元格点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TravelTactic_AreaViewController *areaViewController = [[TravelTactic_AreaViewController alloc] init];
    areaViewController.urlStr = [NSString stringWithFormat:@"http://chanyouji.com/api/destinations/%@.json?page=1", [[[_modelArr objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]ID]];
    [self.navigationController pushViewController:areaViewController animated:YES];
    [areaViewController release];
    
}

#pragma mark 返回分区数量
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _sectionTitleArr.count;
}

#pragma mark 返回分区头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    TravelTactic_MainCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"travelTactic_MainCollectionHeaderView" forIndexPath:indexPath];
    reusableView.titleLabel.text = _sectionTitleArr[indexPath.section];
    return reusableView;
    
    
//    reusableView.titleLabel.text = _sectionTitleArr[indexPath.section];
//    if (indexPath.section == 0) {
//        reusableView.titleLabel.text = _sectionTitleArr[0];
//        reusableView.textWidth = [TravelTactic_MainCollectionReusableView widthOfText:_sectionTitleArr[0] font:14];
//        return reusableView;
//    } else if (indexPath.section == 1) {
//        reusableView.textWidth = [TravelTactic_MainCollectionReusableView widthOfText:_sectionTitleArr[1] font:14];
//        reusableView.titleLabel.text = _sectionTitleArr[1];
//        return reusableView;
//    } else if (indexPath.section == 2) {
//        reusableView.textWidth = [TravelTactic_MainCollectionReusableView widthOfText:_sectionTitleArr[2] font:14];
//        reusableView.titleLabel.text = _sectionTitleArr[2];
//        return reusableView;
//    } else if (indexPath.section == 3) {
//        reusableView.textWidth = [TravelTactic_MainCollectionReusableView widthOfText:_sectionTitleArr[3] font:14];
//        reusableView.titleLabel.text = _sectionTitleArr[3];
//        return reusableView;
//    } else {
//        reusableView.textWidth = [TravelTactic_MainCollectionReusableView widthOfText:_sectionTitleArr[4] font:14];
//        reusableView.titleLabel.text = _sectionTitleArr[4];
//        return reusableView;
//    }
}

#pragma mark 返回分区头视图高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    return CGSizeMake(KWIDTH, 30 * SWIDTH);
    
}

#pragma mark navigationBar透明
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    
//    CGFloat yOffset = scrollView.contentOffset.y;
//    CGFloat yAlpha = ((- 600 * SWIDTH) + yOffset) / (- 600 * SWIDTH);
//    
//    self.navigationController.navigationBar.alpha = 1 - yAlpha;
//    
//}



//- (void)segmentedAction:(UISegmentedControl *)segmented {
//    
//    if (segmented.selectedSegmentIndex == 0) {
//        [self.modelArr removeAllObjects];
//        
//        self.modelArr = _outArr;
//        [self.collectionView reloadData];
//    } else {
//        [self.modelArr removeAllObjects];
//        
//        self.modelArr = _inArr;
//        [self.collectionView reloadData];
//    }
//    
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
